/**
 * @author Roger Wu 
 * @version 1.0
 */
(function($){
	$.fn.cssv = function(pre){
		var cssPre = $(this).css(pre);
		return cssPre.substring(0, cssPre.indexOf("px")) * 1;
	};
	$.fn.jBar = function(options){
		//menu_c_s表示菜单显示与隐藏的切换，changestatus
		var op = $.extend({container:"#container",menucs:"#menu_c_s", collapse:".collapse", toggleBut:".toggleCollapse div", sideBar:"#sidebar", sideBar2:"#sidebar_s", splitBar:"#splitBar", splitBar2:"#splitBarProxy"}, options);
		return this.each(function(){
			var jbar = this;
			//var sbar = $(op.sideBar2, jbar);
			var bar = $(op.sideBar, jbar);
			
			
		    $(op.menucs).click(function () {
		    	
		        if ($('#sidebar > ul').is(":visible") === true) {
		        	DWZ.ui.sbar = false;
		        	 $('.formError').remove();
		        	 
	                $(op.splitBar).hide();
	                //var sbarwidth = sbar.cssv("left") + sbar.outerWidth();
	                
	                var barleft = 0 - bar.outerWidth();
	                var cleft = $(op.container).cssv("left") - bar.outerWidth();
	                var cwidth = bar.outerWidth() + $(op.container).outerWidth();
	                $(op.container).animate({left: cleft,width: cwidth},50,function(){
	                    bar.animate({left: barleft}, 500, function(){
	                        bar.hide();
	                       
	                        $(window).trigger(DWZ.eventType.resizeGrid);
	                    });
	                });

					return false;
		        } else {
		        	DWZ.ui.sbar = true;
		        	    $('.formError').remove();	
		        	   
						bar.show();
						bar.animate({left: 0}, 500, function(){
						$(op.splitBar).show();
									
						var cleft = 5 + bar.outerWidth() + $(op.splitBar).outerWidth();
						var cwidth = $(op.container).outerWidth() - (cleft - $(op.container).cssv("left"));
						$(op.container).css({left: cleft,width: cwidth});
						
						$(window).trigger(DWZ.eventType.resizeGrid);
					});
					return false;
		           
		        }
		    });
			
			
			
			
			$(op.splitBar).mousedown(function(event){
				$(op.splitBar2).each(function(){
					
					var spbar2 = $(this);
					setTimeout(function(){spbar2.show();}, 100);
					spbar2.css({visibility: "visible",left: $(op.splitBar).css("left")});					
					spbar2.jDrag($.extend(options, {obj:$("#sidebar"), move:"horizontal", event:event,stop: function(){
						$(this).css("visibility", "hidden");
						var move = $(this).cssv("left") - $(op.splitBar).cssv("left");
						var sbarwidth = bar.outerWidth() + move;
						var cleft = $(op.container).cssv("left") + move;
						var cwidth = $(op.container).outerWidth() - move;
						bar.css("width", sbarwidth);
						$(op.splitBar).css("left", $(this).css("left"));
						$(op.container).css({left: cleft,width: cwidth});

					}}));
					return false;					
				});
			});
		});
	}
})(jQuery);
